package com.yyj.service;

import com.google.common.collect.Lists;
import com.yyj.common.BaseResponse;
import com.yyj.dao.TPropertyFeeConfigMapper;
import com.yyj.dao.TPropertyManagerMapper;
import com.yyj.dao.TPropertyOwnerMapper;
import com.yyj.enums.UserStatusEnum;
import com.yyj.exception.BaseException;
import com.yyj.model.TPropertyFeeConfig;
import com.yyj.model.TPropertyManager;
import com.yyj.model.TPropertyManagerExample;
import com.yyj.model.TPropertyOwner;
import com.yyj.util.RspUtils;
import com.yyj.util.security.MD5Util;
import com.yyj.web.protocol.req.ManagerAddReq;
import com.yyj.web.protocol.req.ManagerLoginReq;
import com.yyj.web.protocol.req.UserStatusUpdateReq;
import com.yyj.web.protocol.resp.ManagerOwnerResp;
import com.yyj.web.protocol.resp.ManagerResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.annotations.ApiIgnore;

import javax.jws.soap.SOAPBinding;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class ManagerService {

    @Autowired
    private TPropertyManagerMapper managerMapper;

    @Autowired
    private TPropertyOwnerMapper ownerMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private TPropertyFeeConfigMapper feeConfigMapper;

    public TPropertyManager findManagerByMobile(String mobile){
        TPropertyManagerExample ownerExample = new TPropertyManagerExample();
        TPropertyManagerExample.Criteria criteria = ownerExample.createCriteria();
        criteria.andMobileEqualTo(mobile);
        List<TPropertyManager> ownerList = managerMapper.selectByExample(ownerExample);
        if(ownerList != null && ownerList.size() > 0){
            return ownerList.get(0);
        }else{
            return null;
        }
    }

    public List<ManagerOwnerResp> queryManagerOwners(String roomNum){
        List<ManagerOwnerResp> managerOwnerRespList = Lists.newArrayList();
        List<TPropertyOwner> ownerList =  "ALL".equals(roomNum) ? ownerMapper.selectByExample(null) : userService.findOwnersByRoomNum(roomNum);
        for(TPropertyOwner ownerObj : ownerList){
            ManagerOwnerResp managerOwnerResp = new ManagerOwnerResp();
            managerOwnerResp.setMobile(ownerObj.getMobile());
            managerOwnerResp.setStatus(ownerObj.getStatus());
            managerOwnerResp.setUserId(ownerObj.getId());
            managerOwnerResp.setRoomNum(ownerObj.getRoomNum());
            managerOwnerRespList.add(managerOwnerResp);
        }
        return  managerOwnerRespList;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addManager(ManagerAddReq managerAddReq) throws Exception{
        String password = managerAddReq.getPassword();
        String rePassword = managerAddReq.getRePassword();
        TPropertyManager managerObj = findManagerByMobile(managerAddReq.getMobile());
        if(null != managerObj){
            throw new BaseException("手机号已经存在");
        }
        if(!password.equals(rePassword)){
            throw new BaseException("两次输入密码不一致");
        }
        TPropertyManager manager = new TPropertyManager();
        manager.setMobile(managerAddReq.getMobile());
        manager.setPassword(MD5Util.MD5(managerAddReq.getPassword()));
        manager.setIsRoot(0);
        manager.setStatus("VALID");
        manager.setCreateTime(new Date());
        manager.setUpdateTime(new Date());
        managerMapper.insert(manager);
    }

    public List<TPropertyManager> queryValidManagers(){
        TPropertyManagerExample ownerExample = new TPropertyManagerExample();
        TPropertyManagerExample.Criteria criteria = ownerExample.createCriteria();
        criteria.andStatusEqualTo(UserStatusEnum.VALID.getCode());
        List<TPropertyManager> managerList = managerMapper.selectByExample(ownerExample);
        return managerList;
    }

    public List<ManagerResp> queryManagerList(){
        TPropertyManagerExample ownerExample = new TPropertyManagerExample();
        TPropertyManagerExample.Criteria criteria = ownerExample.createCriteria();
        criteria.andIsRootEqualTo(0);
        List<TPropertyManager> managerList = managerMapper.selectByExample(ownerExample);
        List<ManagerResp> managerRespList = Lists.newArrayList();
        for(TPropertyManager propertyManager : managerList){
            ManagerResp managerResp = new ManagerResp();
            BeanUtils.copyProperties(propertyManager , managerResp);
            managerRespList.add(managerResp);
        }
        return managerRespList;
    }

    public List<TPropertyFeeConfig> feeConfigList(){
        return feeConfigMapper.selectByExample(null);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateFee(int id , double fee) throws Exception{
        TPropertyFeeConfig feeConfig = feeConfigMapper.selectByPrimaryKey(id);
        feeConfig.setFee(new BigDecimal(fee));
        feeConfigMapper.updateByPrimaryKeySelective(feeConfig);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateManagerStatus(UserStatusUpdateReq userStatusUpdateReq) throws Exception{
        TPropertyManager propertyManager = managerMapper.selectByPrimaryKey(userStatusUpdateReq.getId());
        propertyManager.setStatus(userStatusUpdateReq.getStatus());
        propertyManager.setUpdateTime(new Date());
        managerMapper.updateByPrimaryKeySelective(propertyManager);
    }

    public BaseResponse login(ManagerLoginReq managerLoginReq) throws Exception {
        TPropertyManager manager = findManagerByMobile(managerLoginReq.getMobile());
        if(null == manager){
            throw new BaseException("管理员不存在");
        }
        String password = MD5Util.MD5(managerLoginReq.getPassword());
        if(!password.equals(manager.getPassword())){
            throw new BaseException("账号或者密码错误");
        }
        manager.setPassword(null);
        return RspUtils.success(manager);
    }

}
